# -*- coding: utf-8 -*-
#
#    Copyright (C) 2012 Gonzalo Bonigo <gonzalobonigo@gmail.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see http://www.gnu.org/licenses/
#


__all__ = [
    'EventFactory',
    'Event',
    'ObservableEvent',
    'UnobservableEvent',
    'SynchronizationEvent',
    'FailEvent'
]


import abc


FIRST_CHARACTER = 0


class EventFactory(object):

    event_type = {}

    def __new__(cls, name):
        assert(type(name) == str)
        assert(len(name) > 0)
        return EventFactory.event_type[name[FIRST_CHARACTER]](name)


class EventRegister(abc.ABCMeta):

    def __new__(cls, clsname, bases, attrs):
        new_cls = super(EventRegister, cls).__new__(cls, clsname, bases, attrs)
        if 'TYPE' in attrs:
            EventFactory.event_type[attrs['TYPE']] = new_cls
        return new_cls


class Event(object):

    class Type(object):
        OBSERVABLE = 'o'
        UNOBSERVABLE = 'u'
        SYNCHRONIZATION = 'c'
        FAIL = 'f'

    __metaclass__ = EventRegister

    def __init__(self, name):
        assert(type(name) == str)
        assert(name.startswith(self.TYPE))
        self.name = name

    def __str__(self):
        return self.name


class ObservableEvent(Event):

    TYPE = Event.Type.OBSERVABLE

    def __init__(self, name):
        super(ObservableEvent, self).__init__(name)


class UnobservableEvent(Event):

    TYPE = Event.Type.UNOBSERVABLE

    def __init__(self, name):
        super(UnobservableEvent, self).__init__(name)


class SynchronizationEvent(Event):

    TYPE = Event.Type.SYNCHRONIZATION

    def __init__(self, name):
        super(SynchronizationEvent, self).__init__(name)


class FailEvent(Event):

    TYPE = Event.Type.FAIL

    def __init__(self, name):
        super(FailEvent, self).__init__(name)
